unit auth_frm;

{$mode objfpc}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls, DbCtrls, datamodule1_frm;

type

  { Tauthform }

  Tauthform = class(TForm)
    btOK: TButton;
    cbutenti: TEdit;
    edreset: TButton;
    edpassword: TLabeledEdit;
    Label1: TLabel;
    procedure btOKClick(Sender: TObject);
    procedure edpasswordKeyPress(Sender: TObject; var Key: char);
    procedure edresetClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  authform: Tauthform;

implementation

{ Tauthform }

procedure Tauthform.btOKClick(Sender: TObject);
begin
//controllo siano stati valorizzati i campi
if cbutenti.text = '' then
   begin
     ShowMessage('Inserire l''utente prima di proseguire');
     FocusControl(cbutenti);
     exit;
   end;
if edpassword.Text='' then
   begin
     ShowMessage('Inserire una password prima di proseguire');
     FocusControl(edpassword);
     exit;
   end;
  //controlo le credenziali e accedo
  with datamodule1.zqutenti do
       begin
         close;
         sql.clear;
         sql.add('SELECT * from Utenti_DB '+
                 'WHERE Utente=:UTENTE');
         ParamByName('UTENTE').AsString:=cbutenti.Text;
         ExecSQL;
         open;
       end;
  with datamodule1.zqutentiaree do
       begin
         close;
         sql.clear;
         sql.add('SELECT * from Utenti_Aree '+
                 'WHERE Utente=:UTENTE');
         ParamByName('UTENTE').AsString:=cbutenti.Text;
         ExecSQL;
       end;
if edpassword.Text=DataModule1.zqutenti.FieldByName('Password').asstring then
   begin
     DataModule1.loginok:=true;
     DataModule1.username:=cbutenti.Text;
     close;
   end
else
    begin
      ShowMessage('Password errata');
      edpassword.Clear;
      FocusControl(edpassword);
      Sleep(1000);
    end;
end;

procedure Tauthform.edpasswordKeyPress(Sender: TObject; var Key: char);
begin
  if key=#13 then
     btOKClick(self);
end;

procedure Tauthform.edresetClick(Sender: TObject);
begin
  //resetto i campi
  cbutenti.Clear;
  edpassword.Clear;
end;

procedure Tauthform.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  CloseAction:=caFree;
end;


procedure Tauthform.FormShow(Sender: TObject);
begin
   DataModule1.loginok:=false;
end;

initialization
  {$I auth_frm.lrs}

end.

